<?php

require_once APPPATH . 'core/controller.php';

class CommentController extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
    }

    public function insert()
    {

        $this->form_validation->set_rules('comment', 'Comment', 'trim|required');


        if ($this->form_validation->run() == FALSE) {
            redirect("post/read/" . $this->input->post('postId'), "location");
        }
        else
        {

            $commentText = $this->input->post('comment');

            $author = $this->em->find("User", $this->session->userdata('userId'));
            $post = $this->em->find("Post", $this->input->post('postId'));

            $comment = new Comment();
            $comment->setAuthor($author);
            $comment->setCommentText($commentText);
            $comment->setPost($post);

            $this->em->persist($comment);
            $this->em->flush();




            redirect("post/read/" . $this->input->post('postId'), "location");
        }
    }
}

?>